﻿@using System.Reflection
@using XCode
@using XCode.Configuration
@using XCode.Membership
@{
    Layout = null;
    //2021年9月30日 照月(zshankang)修改，在非ACE主题下，直接使用 Model.Role 会报错。
    Role role = null;
    if (Model.GetType().GetProperties().Length > 0)
    {
        role = ((System.Reflection.PropertyInfo[])Model.GetType().GetProperties())[0].GetValue(Model, null);
    }
    //var role = Model.Role as Role;
    var menus = Menu.Root.AllChilds;
    //var pfs = EnumHelper.GetDescriptions<PermissionFlags>().Where(e => e.Key > PermissionFlags.None);
}
<script type="text/javascript">
layui.use(['form'], function() {
        var form = layui.form;

        function setCheckbox(parentkey, data){
             $('input[parentkey=' + parentkey + ']').each(function(){
                var name=$(this).attr("name");
                $('input[name=' + name + ']').prop('checked', data.elem.checked).change();
                $('input[name=' + name + ']').val(data.elem.checked);
                
                  var childid=$(this).attr("child");
                  setCheckbox(childid, data);
            });
        }

        function setChildCheckbox(parentkey,data){
            $('input[parentkey=' + parentkey + ']').prop('checked', data.elem.checked).change();
            $('input[parentkey=' + parentkey + ']').each(function(){
                var parentkeyname=$(this).attr("name");
                $('input[name=' + parentkeyname + ']').val(data.elem.checked);
            });
        }

        function setChildCheckboxByUp(parentkey,data){
            $('input[parentkey=' + parentkey + ']').prop('checked', data.elem.checked).change();
            $('input[parentkey=' + parentkey + ']').each(function(){
                var parentkeyname=$(this).attr("name");
                $('input[name=' + parentkeyname + ']').val(data.elem.checked);
                
                  var childid=$(this).attr("child");
                  setChildCheckboxByUp(childid, data);
            });
        }
        function setParentCheckbox(parentkey,data){
            $('input[child=' + parentkey + ']').prop('checked', data.elem.checked).change();
            $('input[child=' + parentkey + ']').each(function(){
                var parentkeyname=$(this).attr("name");
                $('input[name=' + parentkeyname + ']').val(data.elem.checked);
            });
        }

        function setParentCheckboxByUp(parentkey,data){
            $('input[child=' + parentkey + ']').prop('checked', data.elem.checked).change();
            $('input[child=' + parentkey + ']').each(function(){
                var parentkeyname=$(this).attr("name");
                $('input[name=' + parentkeyname + ']').val(data.elem.checked);
                    var parentkeyid=$(this).attr("parentkey");
                    if(!data.elem.checked){
                        
                        var chkArr =  $('input[parentkey=' + parentkeyid + ']');
                        if(chkArr.filter(':checked').length  > 0){
                            return;
                        }
                    }
                     setParentCheckboxByUp(parentkeyid, data);
                    
            });
        }        
        function setViewCheckboxByReadOnly(parentkey,data){
             $('input[parentkey=' + parentkey + ']').each(function(){
            var parentkeyname=$(this).attr("name");
            if($(this).attr("lay-filter") === "authorize"){
                $(this).prop('checked', data.elem.checked).change();
                $(this).each(function(){
                    var parentkeyname=$(this).attr("name");
                    $('input[name=' + parentkeyname + ']').val(data.elem.checked);
                });
            }
            var viewname = parentkeyname.replace('p','pf') + "_1";
            $('input[name='+viewname+']').prop('checked', data.elem.checked).change();
                $('input[name='+viewname+']').each(function(){
                    var parentkeyname=$(this).attr("name");
                    $('input[name=' + parentkeyname + ']').val(data.elem.checked);
                });
            });
        }
        function setCheckboxByReadOnly(childkey,data){
            $('input[child='+childkey+']').prop('checked', data.elem.checked).change();
              $('input[child='+childkey+']').each(function(){
                    var parentkeyname=$(this).attr("name");
                    $('input[name=' + parentkeyname + ']').val(data.elem.checked);
                    var parentkeyid=$(this).attr("child");
                    if(!data.elem.checked){
                        setChildCheckboxByUp(parentkeyid,data);
                    }else{
                        setViewCheckboxByReadOnly(parentkeyid,data);
                    }
                     
                    $('input[parentkey=' + parentkeyid + ']').each(function(){
                         var parentkeyid=$(this).attr("child");
                          setViewCheckboxByReadOnly(parentkeyid,data);
                    });

                    var childid=$(this).attr("parentkey");
                     if(!data.elem.checked){
                        var chkArr =  $('input[parentkey=' + childid + ']');
                        if(chkArr.filter(':checked').length  == 0){
                            setParentCheckbox(childid, data);
                        }
                      }else{
                        setParentCheckbox(childid, data);
                      }

              });
        }
        function setCheckboxByAll(childkey,data){
            $('input[child='+childkey+']').prop('checked', data.elem.checked).change();
              $('input[child='+childkey+']').each(function(){
                    var parentkeyname=$(this).attr("name");
                    $('input[name=' + parentkeyname + ']').val(data.elem.checked);
                    var parentkeyid=$(this).attr("child");
                     setChildCheckboxByUp(parentkeyid,data);
                     
                    var childid=$(this).attr("parentkey");
                     if(!data.elem.checked){
                        var chkArr =  $('input[parentkey=' + childid + ']');
                        if(chkArr.filter(':checked').length  == 0){
                            setParentCheckbox(childid, data);
                        }
                      }else{
                        setParentCheckbox(childid, data);
                      }

                    $('input[parentkey=' + parentkeyid + ']').each(function(){
                         var parentkeyid=$(this).attr("child");
                          setChildCheckboxByUp(parentkeyid,data);
                    });
              });
        }
        // function setReadOnlyOrAllCheckbox(name,data){
        //        $('input[name=' + name + ']').prop('checked', data.elem.checked).change();
        //        $('input[name=' + name + ']').each(function(){
        //            var parentkeyname=$(this).attr("name");
        //            $('input[name=' + parentkeyname + ']').val(data.elem.checked);
                
        //            var id=parentkeyname.split('_')[2];
        //            $('input[child=auth_child' + id + ']').each(function(){
        //                var nameid=$(this).attr("parentkey");
        //                var allname = 'pc_all_' + nameid.replace('auth_child','');
        //                setReadOnlyOrAllCheckbox(allname, data);
        //                var readonlyname = 'pc_readonly_' + nameid.replace('auth_child','');
        //                setReadOnlyOrAllCheckbox(readonlyname, data);
                
        //            });
        //        });
            
        //}

        //授权
        form.on('checkbox(authorize)', function(data){
              $this = $(data.elem);
              var childkey = $this.attr('child');
              var key = $this.attr('name');
              $('input[parentkey=' + childkey + ']').each(function(){
                  var name=$(this).attr("name");
                    $('input[name=' + name + ']').prop('checked', data.elem.checked).change();
                    $('input[name=' + name + ']').val(data.elem.checked);

                    var id = 'auth_child' + $(this).attr("id").replace('p','');
                    setCheckbox(id, data);
                });
              var name=$(this).attr("name");
              $('input[name=' + name + ']').prop('checked', data.elem.checked).change();
              $('input[name=' + name + ']').val(data.elem.checked);
              //设置单个按钮值
              //上级
              var parentkey = $this.attr('parentkey');
              if(!data.elem.checked){
                var chkArr =  $('input[parentkey=' + parentkey + ']');
                if(chkArr.filter(':checked').length  == 0){
                    setParentCheckbox(parentkey, data);
                }
              }else{
                setParentCheckbox(parentkey, data);
              }
              layui.form.render();
        })
        //只读/查看
        form.on('checkbox(pro_detail)', function(data){
              debugger;
              $this = $(data.elem);
              var key = $this.attr('prochildkey');
              var authchild = 'auth_child' + key.split('_')[2];
              //$('input[proparentkey=' + key + ']').prop('checked', data.elem.checked).change();
              var name=$(this).attr("name");
              $('input[name=' + name + ']').prop('checked', data.elem.checked).change();
              $('input[name=' + name + ']').val(data.elem.checked);
              //设置授权按钮
              setCheckboxByReadOnly(authchild,data);
              //$('input[proparentkey=' + key + ']').each(function(){
              //    var parentkey=$(this).attr("parentkey");
              //    setParentCheckbox(parentkey,data);
              //});
              layui.form.render();
        })
        //全部权限
        form.on('checkbox(pro_all)', function(data){
              //console.log(data.elem); //得到checkbox原始DOM对象
              //console.log(data.elem.checked); //是否被选中，true或者false
              //console.log(data.value); //复选框value值，也可以通过data.elem.value得到
              //console.log(data.othis); //得到美化后的DOM对象

              $this = $(data.elem);
              var key = $this.attr('prochildkey');
              
              var authchild = 'auth_child' + key.split('_')[2];
              //$('input[prokey=' + key + ']').prop('checked', data.elem.checked).change();
              var name=$(this).attr("name");
              $('input[name=' + name + ']').prop('checked', data.elem.checked).change();
              $('input[name=' + name + ']').val(data.elem.checked);

             //设置授权按钮
              setCheckboxByAll(authchild,data);
              //$('input[prokey=' + key + ']').each(function(){
              //    var parentkey=$(this).attr("parentkey");
              //    setParentCheckbox(parentkey,data);
              //});
              layui.form.render();
        })

        //单个点击
        form.on('checkbox(pro_child)', function(data){
              //console.log(data.elem); //得到checkbox原始DOM对象
              //console.log(data.elem.checked); //是否被选中，true或者false
              //console.log(data.value); //复选框value值，也可以通过data.elem.value得到
              //console.log(data.othis); //得到美化后的DOM对象

              $this = $(data.elem);
              var name=$(this).attr("name");
              
              $('input[name=' + name + ']').prop('checked', data.elem.checked).change();
               $('input[name=' + name + ']').each(function(){
                    $(this).val(data.elem.checked);
                });
              //赋值授权按钮
              var parentkey=$(this).attr("parentkey");
                if(name.split('_')[1] === '1' && !data.elem.checked){
                    setParentCheckbox(parentkey,data);
                    setChildCheckbox(parentkey,data);
                    var parentkey=$('input[child=' + parentkey + ']').attr("parentkey");
                     var chkArr =  $('input[parentkey=' + parentkey + ']');
                    if(chkArr.filter(':checked').length  == 0){
                        setParentCheckboxByUp(parentkey,data);
                    }
                }else{
                    
                    if(data.elem.checked){
                        var firstName = name.split('_')[0] + '_1' ;
                       $('input[name=' + firstName + ']').prop('checked', data.elem.checked).change();
                       $('input[name=' + firstName + ']').each(function(){
                            $(this).val(data.elem.checked);
                        });
                        
                        setParentCheckbox(parentkey,data);
                        
                        var parentkey=$('input[child=' + parentkey + ']').attr("parentkey");
                        
                        setParentCheckboxByUp(parentkey,data);

                    }else{
                        var chkArr =  $('input[parentkey=' + parentkey + ']');
                        if(chkArr.filter(':checked').length  == 0){
                            setParentCheckboxByUp(parentkey,data);
                        }
                    }
                }
              layui.form.render();
        })
})

</script>
    <label class="layui-form-label">授权</label>
    <div class="layui-input-block">
        <table lay-filter="parse-authorize" id="parse-authorize">
            <thead>
                <tr>
                    <th lay-data="{field:'TreeNodeName',title:'名称'}">名称</th>
                    <th lay-data="{field:'DisplayName',title:'显示名',width:150}">显示名</th>
                    <th lay-data="{field:'TreeNodeName2',title:'授权',width:80}">授权</th>
                    <th lay-data="{field:'TreeNodeName3',title:'操作权限',width:750}">操作权限</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var entity in menus)
                {
                    var isShow = true;
                    if (entity.TreeNodeName.Equals("Admin") || entity.TreeNodeName.Equals("Cube"))
                    {
                         isShow = false;
                    }
                    else
                    {
                        foreach (var parent in entity.AllParents)
                        {
                            if (parent.TreeNodeName.Equals("Admin") || parent.TreeNodeName.Equals("Cube"))
                            {
                                isShow = false;
                                break;
                            }
                        }
                    }
                    if (role.Name != "超级管理员" && !isShow) continue;
                <tr>
                        <td>@entity.TreeNodeName</td>
                        <td>@entity.DisplayName</td>
                        <td>@Html.CheckBox("p" + entity.ID, role.Has(entity.ID), new { @class = "authorize", @child = "auth_child" + entity.ID, @parentkey = "auth_child" + entity.ParentID,lay_skin="primary",lay_filter="authorize"})
                        <script type="text/javascript">
                                    $('input[name=p@(entity.ID)]').val("@(role.Has(entity.ID).ToString().ToLower())");
                        </script>
                        </td>
                        <td>
                            @if (entity.Childs.Count == 0)
                            {
                                foreach (var item in entity.Permissions.OrderBy(e => e.Key))
                                {
                                    var id = "pf" + entity.ID + "_" + ((Int32)item.Key);
                                    @Html.CheckBox(id, role.Has(entity.ID, (PermissionFlags)item.Key), new { @parentkey = "auth_child" + entity.ID, @proparentkey = "pro_" + item.Key + "_" + entity.ParentID, @prokey = "pro_" + (UInt32)PermissionFlags.All + "_" + entity.ParentID,title=item.Value,lay_filter="pro_child"})
                                @*@Html.Label(id, item.Value)*@
                                @*var chec = role.Has(entity.ID, (PermissionFlags)item.Key)?"checked":"";*@
                                <script type="text/javascript">
                                    $('input[name=@id]').val("@(role.Has(entity.ID, (PermissionFlags)item.Key).ToString().ToLower())");
                                </script>
                                    <text>&nbsp;</text>
                                }
                            }
                            else
                            {

                                @Html.CheckBox("pc_readonly_" + entity.ID, false, new { @class = "pro_detail", @prochildkey = "pro_" + (Int32)PermissionFlags.Detail + "_" + entity.ID,title="只读",lay_skin="primary",lay_filter="pro_detail"})
                                @*@Html.Label("pc_readonly_" + entity.ID, "只读")*@
                                <text>&nbsp;</text>
                                @Html.CheckBox("pc_all_" + entity.ID, false, new { @class = "pro_all", @prochildkey = "pro_" + (UInt32)PermissionFlags.All + "_" + entity.ID,title="全部",lay_skin="primary",lay_filter="pro_all" })
                                @*@Html.Label("pc_all_" + entity.ID, "全部")*@
                            }
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>

<style>
    .layui-table-cell .layui-form-checkbox[lay-skin=primary]{
        padding-left:30px;
    }
</style>